﻿package mortal.game.view.guild.redPack
{
    import Message.Game.*;
    import Message.Public.*;
    import extend.language.*;
    import mortal.common.tools.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.manager.msgTip.*;
    import mortal.game.mvc.*;
    import mortal.game.view.chat.chatPrivate.*;
    import mortal.mvc.core.*;

    public class RedPackController extends Controller
    {

        public function RedPackController()
        {
            return;
        }// end function

        override protected function initServer() : void
        {
            NetDispatcher.addCmdListener(ServerCommand.RedPacketCanGive, this.showRedPackIcon);
            NetDispatcher.addCmdListener(ServerCommand.RedPacketHasGiven, this.showRedPackIcon);
            NetDispatcher.addCmdListener(ServerCommand.RedPacketGetResult, this.RedPackRobResult);
            NetDispatcher.addCmdListener(ServerCommand.RedPacketShowGetInfo, this.updateRobInfo);
            NetDispatcher.addCmdListener(ServerCommand.NewDayUpdate, this.newDayUpdate);
            Dispatcher.addEventListener(EventName.RedPackGet, this.redPackOpen);
            Dispatcher.addEventListener(EventName.RedPackRob, this.redPackRob);
            Dispatcher.addEventListener(EventName.RedPackSelfGiveWinOpen, this.redPackSelfGive);
            Dispatcher.addEventListener(EventName.RedPackActiveOpen, this.redPackActiveOpen);
            Dispatcher.addEventListener(EventName.ActiveStart, this.activesStartHandler);
            Dispatcher.addEventListener(EventName.ActiveNotic, this.activesNoticHandler);
            Dispatcher.addEventListener(EventName.ActiveEnd, this.activesEndHandler);
            return;
        }// end function

        private function showRedPackIcon(param1:Object) : void
        {
            var _loc_2:* = param1 as SRedEnvelopeInfo;
            if (_loc_2 && _loc_2.type == 1)
            {
                RedPackSelfIcon.instance.show();
                ClockManager.instance.addDateCall(_loc_2.effectDay, this.removeRedPackSelfIco);
            }
            else
            {
                RedPackGuildIcon.instance.show();
            }
            return;
        }// end function

        private function removeRedPackSelfIco() : void
        {
            if (RedPackSelfIcon.isViewShow)
            {
                RedPackSelfIcon.instance.hide();
            }
            if (RedPacketWin.isViewShow)
            {
                RedPacketWin.instance.hide();
            }
            Cache.instance.redPackCache.selfRedEnvelopeInfo = null;
            return;
        }// end function

        private function RedPackRobResult(param1:SRedEnvelopeInfo) : void
        {
            if (RedPacketWin.isViewShow)
            {
                RedPacketWin.instance.updateResult(param1);
            }
            else
            {
                RedPacketWin.instance.show();
                RedPacketWin.instance.updateResult(param1);
            }
            return;
        }// end function

        private function redPackOpen(event:DataEvent) : void
        {
            var _loc_2:* = event.data as RedPackData;
            Cache.instance.redPackCache.redPackState = RedPackStateType.RedPackOpen;
            GameProxy.guild.getRedEnvelopeInfo(_loc_2.packId);
            return;
        }// end function

        private function redPackRob(event:DataEvent) : void
        {
            var _loc_2:* = event.data as int;
            GameProxy.guild.robRedEnvelope(_loc_2);
            return;
        }// end function

        private function redPackSelfGive(event:DataEvent) : void
        {
            if (Cache.instance.redPackCache.selfRedEnvelopeInfo)
            {
                Cache.instance.redPackCache.redPackState = RedPackStateType.SelfeSetRedPack;
                GameProxy.guild.getRedEnvelopeInfo(Cache.instance.redPackCache.selfRedEnvelopeInfo.redId);
            }
            else
            {
                Cache.instance.redPackCache.redPackState = RedPackStateType.SelfeSetRedPack;
                RedPacketWin.instance.show();
                RedPacketWin.instance.updateType();
            }
            return;
        }// end function

        private function updateRobInfo(param1:Object) : void
        {
            var _loc_2:* = param1 as SRedEnvelopeInfo;
            if (RedPacketWin.isViewShow)
            {
                RedPacketWin.instance.updateType(_loc_2);
            }
            else
            {
                RedPacketWin.instance.show();
                RedPacketWin.instance.updateType(_loc_2);
            }
            return;
        }// end function

        private function newDayUpdate(param1:Object) : void
        {
            Cache.instance.redPackCache.guildRedEnvelopeInfo = null;
            if (ChatManager.guildWindow)
            {
                ChatManager.guildWindow.cancelTopTipHtmlText();
            }
            if (RedPacketWin.isViewShow)
            {
                RedPacketWin.instance.hide();
            }
            if (RedPackGuildIcon.isViewShow)
            {
                RedPackGuildIcon.instance.hide();
            }
            return;
        }// end function

        private function redPackActiveOpen(event:DataEvent) : void
        {
            var _loc_3:RedPackData = null;
            var _loc_2:* = Cache.instance.redPackCache.activeEnvelopeInfo;
            if (_loc_2)
            {
                _loc_3 = new RedPackData();
                _loc_3.bossName = _loc_2.boss.name;
                _loc_3.packId = _loc_2.redId;
                Dispatcher.dispatchEvent(new DataEvent(EventName.RedPackGet, _loc_3));
            }
            else
            {
                MsgManager.showRollTipsMsg(Language.getString(35632), MsgRollTipsType.msgRollTips3);
            }
            return;
        }// end function

        private function activesStartHandler(event:DataEvent) : void
        {
            var _loc_2:* = DateParser.strToDateNormal("2015-11-23 23:59:00.000");
            var _loc_3:* = DateParser.strToDateNormal("2015-11-28 23:59:00.000");
            var _loc_4:* = ClockManager.instance.nowDate;
            if (_loc_2.time >= _loc_4.time || _loc_4.time >= _loc_3.time)
            {
                return;
            }
            var _loc_5:* = event.data as SClientActive;
            if (_loc_5.type == EActiveType._EActiveTypeThanksGivingRed)
            {
                RedPackActiveIcon.instance.show();
                RedPackActiveIcon.instance.setLeftTimeAndState((_loc_5.endDt.time - ClockManager.instance.nowDate.time) * 0.001, 2);
                RedPackActiveIcon.instance.playEffect();
            }
            return;
        }// end function

        private function activesNoticHandler(event:DataEvent) : void
        {
            var _loc_2:* = DateParser.strToDateNormal("2015-11-23 23:59:00.000");
            var _loc_3:* = DateParser.strToDateNormal("2015-11-28 23:59:00.000");
            var _loc_4:* = ClockManager.instance.nowDate;
            if (_loc_2.time >= _loc_4.time || _loc_4.time >= _loc_3.time)
            {
                return;
            }
            var _loc_5:* = event.data as SClientActive;
            if (_loc_5.type == EActiveType._EActiveTypeThanksGivingRed)
            {
                RedPackActiveIcon.instance.show();
                RedPackActiveIcon.instance.setLeftTimeAndState((_loc_5.startDt.time - ClockManager.instance.nowDate.time) * 0.001, 1);
                RedPackActiveIcon.instance.playEffect();
            }
            return;
        }// end function

        private function activesEndHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as SClientActive;
            if (_loc_2.type == EActiveType._EActiveTypeThanksGivingRed)
            {
                if (RedPackActiveIcon.isViewShow)
                {
                    RedPackActiveIcon.instance.hide();
                }
            }
            return;
        }// end function

    }
}
